package com.yao.code;

/**
 * @author yaoquanwei
 * @date 2021/8/11 11:52
 */
public class Code210811 {

    /**
     * 剑指offer 03.数组中重复的数字
     *
     * @param nums
     * @return
     */
    public static int findRepeatNumber(int[] nums) {
        int num[] = new int[nums.length];
        for (int i : nums) {
            num[i]++;
            if (num[i] > 1) {
                return i;
            }
        }
        return -1;
    }

    /**
     * 剑指offer 53.在排序数组中查找数字
     *
     * @param nums
     * @param target
     * @return
     */
    public static int search(int[] nums, int target) {
        int left = 0, right = nums.length - 1;
        int res = 0;
        while (left < right) {
            int mid = (left + right) / 2;
            if (nums[mid] < target) {
                left = mid + 1;
            } else {
                right = mid;
            }
        }
        while (left < nums.length && nums[left++] == target) {
            res++;
        }
        return res;
    }


    /**
     * 剑指offer 53.0~n-1中缺失的数字
     *
     * @param nums
     * @return
     */
    public static int missingNumber(int[] nums) {
        int left = 0, right = nums.length - 1;
        while (left < right) {
            int mid = (left + right) / 2;
            if (nums[mid] != mid) {
                right = mid;
            } else {
                left = mid + 1;
            }
        }
        if (left == nums.length - 1 && nums[left] == left) {
            return left + 1;
        } else {
            return left;
        }
    }

    public static void main(String[] args) {

    }
}
